package com.xiaozhi.utils;

import cn.hutool.core.util.IdUtil;
import com.xiaozhi.VO.CourseVo;
import com.xiaozhi.VO.LoginVO;
import com.xiaozhi.entity.Course;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

public class CommonUtil {

    /**
     * 获取token
     */
    public static String getCurrentToken() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes()))
                .getRequest();
        return request.getHeader("token");
    }

    /**
     * 创建Token
     */
    public static String createToken(String type,String account,String password){

        return type+"_"+account+"_"+password+"_"+IdUtil.simpleUUID().substring(0,5);
    }

    /**
     * 解析Token信息
     */
    public static LoginVO getTokenInfo(String token){
        LoginVO loginVO = new LoginVO();
        if (token==null)
            return loginVO;
        String[] s = token.split("_");
        loginVO.setUserType(s[0]);
        loginVO.setAccount(s[1]);
        loginVO.setPassword(s[2]);
        return loginVO;
    }
    /*
    * 将课程的年级进行格式转换
    *
    * */

    public static String getGrade(int grade){
//       return switch(grade){
//           case 1 -> "一年级";
//           case 2->"二年级";
//           case 3->"三年级";
//           case 4->"四年级";
//           case 5->"五年级";
//           case 6->"六年级";
//           default->
//              "初高中";
//       };
        if(grade==1)return "一年级";
        if(grade==2)return "二年级";
        if(grade==3)return "三年级";
        if(grade==4)return "四年级";
        if(grade==5)return "五年级";
        if(grade==6)return "六年级";
        return "初高中";
    }
public static List<CourseVo<Course>> addData(List<Course> courses){
    List<CourseVo<Course>> listResults=new ArrayList<>();
    HashSet<Integer> set=new HashSet<>();
    for (Course course : courses) {
        if(set.contains(course.getGrade())){
            continue;
        }
        set.add(course.getGrade());
        CourseVo<Course> courseVo=new CourseVo<>();
        courseVo.setTitle(CommonUtil.getGrade(course.getGrade()));
        courseVo.setDatas(courses.stream().filter(g->g.getGrade()==course.getGrade()).toArray(Course[]::new));
        listResults.add(courseVo);
    }
    return listResults;
}
}
